﻿seajs.use "/init", (init) ->
	fix = (box) ->
		init.fix_chosen(box)
		init.fix_txttime_bt(box)

	$ = init.jq
	$ ->
		fix $('body')

		inp_ImageIDs = $("#ImageIDs")
		con_uploader = $("#uploader")
		btn_submit = $("button[type='submit']")

		con_uploader.pluploadQueue
			runtimes: 'flash,silverlight,html5,html4'
			url: 'http://localhost:38149/photo/uploadfiles'
			max_file_size: '10mb'
			unique_names: true
			flash_swf_url: '/front_net/resource/plupload/plupload.flash.swf'
			silverlight_xap_url: '/front_net/resource/plupload/plupload.silverlight.xap'
			multiple_queues: true
			headers: { userKey: $("#UserKey").text().trim() } #值不得有换行符，坑爹！
			filters :[
				title : "Image", extensions : "jpg,gif,png"
			]
			preinit:
				Init: (up, info)->
					$("div.plupload_header").remove()
					#$("a.plupload_start").remove()
			init:
				StateChanged: (up)->
					#if up.files.length == (up.total.uploaded + up.total.failed)
					#	con_uploader.data("uploaded", true)
					#	btn_submit.click()
				FileUploaded: (up, file, info)->
					response = init.JSON.parse(info.response);
					inp_ImageIDs.val if inp_ImageIDs.val() then inp_ImageIDs.val() + "," + response.imageIDs else response.imageIDs

		btn_submit.on 'click', ->
			#if con_uploader.data("uploaded")
			#	return true
			if inp_ImageIDs.val() && inp_ImageIDs.val() != "0"
				return true
			#uploader = con_uploader.pluploadQueue()
			#if !uploader.files.length
			#	alert('你必须选择至少一张图片。')
			#	return false
			#uploader.start()
			alert('你必须选择至少一张图片。')
			return false